package com.cyt.sys.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.sys.model.po.SysMenu;
import com.cyt.sys.dao.SysUserRoleDao;
import com.cyt.sys.model.po.SysUserRole;
import com.cyt.sys.model.vo.SysUserRoleVo;
import com.cyt.sys.service.SysUserRoleService;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @author changzheng hou
 * @return
 * @date 2019/4/2 16:20
 */
@Service("baseUserRoleService")
public class SysUserRoleServiceImpl extends BaseServiceImpl<SysUserRole> implements SysUserRoleService {

    @Resource
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    public void setBaseDao(SysUserRoleDao sysUserRoleDao) {
        this.baseDao = sysUserRoleDao;
    }

    /**
     * 保存或更新
     *
     * @param sysUserRoleVo
     * @author changZheng hou
     * @date 2019/4/2 16:19
     */
    public void addOrUpdate(SysUserRoleVo sysUserRoleVo) {
        //先删除对象
        sysUserRoleDao.deleteByUserId(sysUserRoleVo.getUserId());
        if (StringUtil.isNotEmpty(sysUserRoleVo.getUserId())) {
            String[] ids = sysUserRoleVo.getRoleIds().split(",");
            for (String id : ids) {
                sysUserRoleDao.insert(new SysUserRole(sysUserRoleVo.getUserId(), id));
            }
        }

    }

    /**
     * 增加或修改角色的用户
     *
     * @return ReturnResult
     * @author changZheng hou
     * @date 2019/4/8 16:50
     */
    public ReturnResult UpdateUser(String roleId, String ruleIds) {
        if (StringUtil.isNotEmpty(roleId)) {
            sysUserRoleDao.deleteByRoleId(roleId);
            String[] ids = ruleIds.split(",");
            List<String> list = new ArrayList<>();
            for(int i=0;i<ids.length;i++){
                if(!list.contains(ids[i])){
                    list.add(ids[i]);
                }
            }
            for (String userId : list) {
                sysUserRoleDao.insert(new SysUserRole(userId, roleId));
            }
        }
        return new ReturnResult();
    }


    /**
     * 查询对象集合
     *
     * @param sysUserRoleVo 对象
     * @return
     * @author shengQiang yu
     * @date 2019-04-23 17:04
     */
    public List<SysUserRole> listExample(SysUserRoleVo sysUserRoleVo) {
        Example example = new Example(SysMenu.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty( sysUserRoleVo.getRoleId())) {
            criteria.andEqualTo("roleId", sysUserRoleVo.getRoleId());
        }
        if (StringUtil.isNotEmpty( sysUserRoleVo.getUserId())) {
            criteria.andEqualTo("userId", sysUserRoleVo.getUserId());
        }
        List<SysUserRole> list = sysUserRoleDao.selectByExample(example);
        return list;
    }

}